package com.squirrel.michale;

public class Day20240502_LeetCode003133 {

    public long minEnd(int n, int x) {

        n--; // 先把 n 减一，这样下面讨论的 n 就是原来的 n-1
        long ans = x;
        int i = 0, j = 0;
        while ((n >> j) > 0) {
            // x 的第 i 个比特值是 0，即「空位」
            if ((ans >> i & 1) == 0) {
                // 空位填入 n 的第 j 个比特值
                ans |= (long) (n >> j & 1) << i;
                j++;
            }
            i++;
        }
        return ans;


    }


    public static void main(String[] args) {

        int n = 3 ;
        int x = 4;

        Day20240502_LeetCode003133 leetCode003133 = new Day20240502_LeetCode003133();

        long l = leetCode003133.minEnd(n, x);
        System.out.printf(String.valueOf(l));

        System.out.println("===================");


        int num = 906413;
        print(num);
    }

    public static void print(int num) {
        for (int i = 31; i >=0 ; i--) {
            System.out.print((num&(1<<i))==0?"0":"1");
        }
        System.out.println();
    }


}
